// 创建班级相关的小仓库
import { defineStore } from "pinia";
import type { ClassState } from "./types/types";
import type { classAllResponseData, classInfoData, classListFormData, classListResponseData, classResponseData } from "@/api/class/type";
import { reqClassAdd, reqClassAll, reqClassById, reqClassDelete, reqClassList, reqClassUpdata } from "@/api/class";




let useClassStore = defineStore("Class", {
    state: (): ClassState => {
        return {
           classes: []
        }
    },
    actions: {
        // 获取所有班级
        async getClassAll() {

            let result:  classAllResponseData = await reqClassAll()
            if (result.status == 0) {
                this. classes = (result.data as  classInfoData[])

                return 'ok'
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 分页获取所有班级
        async getClassList(data:  classListFormData) {

            let result:  classListResponseData = await reqClassList(data);
            if (result.status == 0) {
                this. classes = (result.data?.data as  classInfoData[])

                return result.data
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 添加班级
        async addClass(data:  classInfoData) {

            let result:  classResponseData = await reqClassAdd(data);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 查找所有班级信息（通过id）
        async getClassById(_id: string) {

            let result:  classResponseData = await reqClassById(_id);
            if (result.status == 0) {
                return result.data
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        //  修改班级信息
        async upDataClass(data: classInfoData) {

            let result:  classResponseData = await reqClassUpdata(data);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 删除班级
        async deleteClass( classId: string) {

            let result:  classResponseData = await reqClassDelete( classId);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
    }
})
export default useClassStore